package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ActActivity extends AppCompatActivity {

    private String acid,name;
    private Button sign,publish;
    private TextView tv;

    private ListView lv;
    private EditText et;
    private boolean ifadmin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act);
//        Bundle bundle=getIntent().getExtras();
//        acid=bundle.getString("acid");
//        name=bundle.getString("name");
        sign=findViewById(R.id.btn_act_1);
        publish=findViewById(R.id.btn_act_2);
        et=findViewById(R.id.et_act_1);
        sign.setVisibility(View.GONE);
        tv=findViewById(R.id.tv_act_1);
        lv=findViewById(R.id.list_act_view);
        //tv.setText("欢迎,"+name);
        String success="发送成功";
        init();

        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //签到!!!
                Intent intent = new Intent(ActActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String strtalk=et.getText().toString();
                        HttpURLConnection talkcon=null;
                        String urltalk="http://10.128.48.203:8000/talk/";
                        try {
                            URL requrl=new URL(urltalk);
                            talkcon=(HttpURLConnection)requrl.openConnection();
                            talkcon.setRequestMethod("POST");
                            talkcon.setReadTimeout(5000);
                            talkcon.connect();
                            OutputStream out = talkcon.getOutputStream();
                            String content="context="+strtalk+"&poster="+name+"&id="+acid;
                            out.write(content.getBytes());
                            out.flush();
                            out.close();
                            if(talkcon.getResponseCode()==200){
                                Toast.makeText(ActActivity.this,success,Toast.LENGTH_SHORT).show();
                            }
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
    private void init(){
        String[] names={"班长  ","班长  ","学生a ","学生b ","学生a "};
        String[] contents={"明天七点集合","先清点好人数再出发","是用掌纹签到吗？","是的","好的明白了，谢谢"};
        String[] times={"4.16 20:41","4.16 20:42","4.16 21:07","4.16 21:11","4.16 21:13"};
        List<Talk>talkList = new ArrayList<>();
        for(int i=0;i<5;i++){
            Talk tal=new Talk(contents[i],names[i],times[i]);
            talkList.add(tal);
        }
        TalkAdapter talkAdapter = new TalkAdapter(ActActivity.this,R.layout.act_layout,talkList);
        lv.setAdapter(talkAdapter);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                String result;
//                BufferedReader buf=null;
//                String url="http://10.128.48.203:8000/activity/?id="+acid;
//                HttpURLConnection actcon = null;
//                try{
//                    URL requesturl= new URL(url);
//                    actcon=(HttpURLConnection)requesturl.openConnection();
//                    actcon.setRequestMethod("GET");
//                    actcon.setReadTimeout(5000);
//                    actcon.connect();
//
//                    if(actcon.getResponseCode()==200){
//                        InputStream inputs=actcon.getInputStream();
//                        //将二进制流包装
//                        buf =new BufferedReader(new InputStreamReader(inputs));
//                        //读取字符串
//                        String line;
//                        StringBuilder builder = new StringBuilder();
//                        while((line=buf.readLine())!=null){
//                            builder.append(line);
//                            builder.append("\n");
//                        }
//                        result=builder.toString();
//                        JSONObject jsonObject = new JSONObject(result);
//                        String des=jsonObject.optString("description");
//                        JSONArray admins = jsonObject.optJSONArray("admins");
//                        ifadmin=false;
//                        String test;
//                        for(int i=0;i<admins.length();i++){
//                            test=(String)admins.opt(i);
//                            if(name.equals(test)){
//                                ifadmin=true;
//                            }
//                        }
//                        if(ifadmin){
//                            sign.setVisibility(View.VISIBLE);
//                        }
//                        JSONArray talks=jsonObject.getJSONArray("talks");
//                        String talk,id;
//                        List<Suborg>talkList = new ArrayList<>();
//                        for(int i=0;i<talks.length();i++){
//                            JSONObject t=talks.optJSONObject(i);
//                            talk=t.optString("context");
//                            id=t.getString("id");
//                            Suborg tal=new Suborg(talk,id);
//                            talkList.add(tal);
//                        }
//                        SuborgAdapter talkAdapter = new SuborgAdapter(ActActivity.this,R.layout.subo_layout,talkList);
//                        lv.setAdapter(talkAdapter);
//
//                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                            @Override
//                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//                                Suborg suborg= talkList.get(position);
//                                String thetalk=suborg.soname;
//
//                                //show the full talk
//                                Toast toasttalk=Toast.makeText(getApplicationContext(),thetalk,Toast.LENGTH_SHORT);
//                                toasttalk.setGravity(Gravity.CENTER,0,0);
//                                toasttalk.show();
//                            }
//                        });
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
//        }).start();
    }
}